//
//  DHNetworkManager.m
//  宏达APP
//
//  Created by DIANGHONG on 2018/4/27.
//  Copyright © 2018年 123. All rights reserved.
//

#import "DHNetworkManager.h"
#import <AFHTTPSessionManager.h>


//网络工具类单例
@implementation DHNetworkManager

// 用户账户全局只有一个
- (DHUserAccount *)userAccount{
    if (!_userAccount) {
        _userAccount = [[DHUserAccount alloc] init];
    }
    return _userAccount;
}

+ (instancetype)shareManager{
    
    static id instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[DHNetworkManager alloc] init];
        
       [(DHNetworkManager *)instance responseSerializer].acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/json", nil];
        
    });
    return instance;
}

/*
 封装上传数据的方法
 */
- (void)uploadWithUrlString:(NSString *)urlString dataList:(NSArray *)dataList nameList:(NSArray *)nameList params:(NSDictionary *)params completion:(void(^)(id json, BOOL isSuccess))completion
{
    
    
    //post请求
    [self POST:urlString parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        [formData appendPartWithFileData:dataList.firstObject name:nameList.firstObject fileName:@"xxx1" mimeType:@"image/jpeg"];
        [formData appendPartWithFileData:dataList.lastObject name:nameList.lastObject fileName:@"xxx2" mimeType:@"image/jpeg"];

        //拼接结构体
        //[formData appendPartWithFileData:data name:name fileName:@"xxx" mimeType:@"application/octet-stream"];
//        [dataList enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//
//
//        }];
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        completion(responseObject,true);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //创建NSHTTPURLResponse
        NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
        
        //判断token过期//错误处理
        if (response.statusCode == 403) {
            // token过期处理 通知用户重新登录
           // [NSNotificationCenter.defaultCenter postNotificationName:DHUserShouldLogonNotification object:@"tokenIsBad"];
            
            NSLog(@"%@",error);
            
        }
        
        completion(nil , false);
        
    }];
}
/*
 用一个函数网络封装请求GET、POST的方法
 **/
- (void)requestWithMethod:(MethodHTTP)method UrlString:(NSString *)urlStr params:(NSDictionary *)params completion:(void(^)(id json ,BOOL isSuccess))completion{
    
    // 成功回调
    void(^successful)(id, NSURLSessionDataTask *) = ^(id response, NSURLSessionDataTask *task){
        
        completion(task,true);
    };
    
    // 是失败回调
    void(^requestFailure)(NSURLSessionDataTask *, NSError *) = ^(NSURLSessionDataTask *task, NSError *error){
        completion(nil,false);
        
        //FIXME: 网络请求失败
        NSLog(@"失败---%@",error);
    };
    
    
    if (method == MethodGet) {
    
        [self GET:urlStr parameters:params progress:nil success:successful failure:requestFailure];
        
    }else{
        
        [self POST:urlStr parameters:params progress:nil success:successful failure:requestFailure];
    }
    
}

@end
